package chances.wechat.gateway;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import chances.wechat.controller.UserAbstractController;
import chances.wechat.entity.QrCodeInfo;
import chances.wechat.entity.StbInfo;
import chances.wechat.entity.WechatStbBind;
import chances.wechat.entity.WechatUser;
import chances.wechat.msggw.context.BindContext;
import chances.wechat.msggw.resp.BaseResponse;
import chances.wechat.msggw.resp.BindResponse;
import chances.wechat.resp.ResultResponse;

/*@RestController
@RequestMapping("/user")*/
public class UserController extends UserAbstractController {

	@RequestMapping("/bind")
	public ResultResponse<String> bind(HttpServletResponse response, 
			@RequestParam("user_id") String openId,
			@RequestParam("qr_info") String qrInfo) throws IOException {
		return deviceBind(openId, qrInfo);
	}
	
	public ResultResponse<String> deviceBind(String userId, String code) {
		ResultResponse<String> result = new ResultResponse<String>();
		QrCodeInfo qrCodeInfo = qrCodeRepository.getQrCodeByQrInfo(code);
		StbInfo stbInfo = this.userRepository.findStbInfo(qrCodeInfo.getStbNo());
		if (stbInfo == null) {
			stbInfo = this.createStbEntity(qrCodeInfo);
			this.userRepository.insertStbInfo(stbInfo);
		}
		
		WechatStbBind userBind = this.bindService.query(userId, qrCodeInfo.getStbNo());
		result.setStatus(SUCCESS);
		if(userBind == null) {
			try {
				WechatUser user = this.userRepository.getWechatUser(userId);
				String qrcodeurl = this.getBindUrl(qrCodeInfo);
				BindResponse bindResponse = this.bindQr(user, qrcodeurl);
				userBind = this.userRepository.bind(user, stbInfo, bindResponse.getBindid());
				result.setMessage(bindResponse.getContent());
			} catch (Throwable ex) {
				ex.printStackTrace();
				result.setStatus(DB_ERROR);
			}
		} else {
			this.updateBindStatus(userBind);
		}
		return result;
	}

	@RequestMapping("/unbind")
	public ResultResponse<String> unbind(HttpServletResponse response, 
			@RequestParam("user_id") String openId,
			@RequestParam("stb_no") String stbNo) throws IOException {

		ResultResponse<String> result = new ResultResponse<String>();
		WechatStbBind bind = this.bindService.query(openId, stbNo);
		try {
			if (bind != null) {
				this.bindService.delBind(bind.getBindId());
			}
			BaseResponse unbind = unbind(openId, bind);
			result.setStatus(SUCCESS);
			result.setMessage(unbind.getContent());
		} catch (Throwable ex) {
			result.setStatus(SYS_ERROR);
		}

		return result;
	}

	private BaseResponse unbind(String userId, WechatStbBind weixinBind) throws RemoteException {
		BindContext bindContext = new BindContext();
		bindContext.setCmd("unbind");
		bindContext.setAppacct(userId);
		bindContext.setBindid(weixinBind.getRoleName());
		BaseResponse bindResponse = this.bindService.unBind(bindContext);
		return bindResponse;
	}

	@RequestMapping("/list_bind")
	public ResultResponse<List<WechatStbBind>> listBind(HttpServletResponse response, 
			@RequestParam("user_id") String openId)
			throws IOException {
		ResultResponse<List<WechatStbBind>> result = new ResultResponse<List<WechatStbBind>>();
		try {
			List<WechatStbBind> bindList = this.bindService.listBind(openId);
			result.setStatus(SUCCESS);
			result.setResult(bindList);
		} catch (Throwable ex) {
			result.setStatus(DB_ERROR);
		}
		return result;
	}

}
